<?php

namespace app\api\library;
use think\exception\Handle;
use think\cache;


class Wechat extends Handle
{
    private static $appid = 'wx5e72685318853998';
    private static $secret = 'a8529bd98955df57674633324262c226';

    //获取accesstoken
    public static function getAccessToken()
    {
        $access_token = Cache::get('laner_wechat_access_token');
        if(empty($access_token)){
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".self::$appid."&secret=".self::$secret;
            // 微信返回的信息
            $returnData = json_decode(self::curlHttp($url));
            $access_token = $returnData->access_token;
            Cache::set('laner_wechat_access_token',$access_token,7100);
        }
        return $access_token;
    }

    public static function getJsApiTicket($accessToken) {
        $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$accessToken."&type=jsapi";
        // 微信返回的信息
        $returnData = json_decode(self::curlHttp($url));
       // var_dump($returnData);
        $resData['ticket'] = $returnData->ticket;
        $resData['expiresIn'] = $returnData ->expires_in;
        $resData['time'] = date("Y-m-d H:i",time());
        $resData['errcode'] = $returnData->errcode;
        return $resData;
    }

    public static function getSignPackage($url) {
        // 获取token
        $access_token =self::getAccessToken();
        // 获取ticket
        $ticketList = self::getJsApiTicket($access_token);
        $ticket = $ticketList['ticket'];
        // 生成时间戳
        $timestamp = time();
        // 生成随机字符串
        $nonceStr =self::createNoncestr();
        // 这里参数的顺序要按照 key 值 ASCII 码升序排序 j -> n -> t -> u
        $string = "jsapi_ticket=".$ticket."&noncestr=".$nonceStr."&timestamp=".$timestamp."&url=".$url;
        $signature = sha1($string);
        $signPackage = array (
            "appId" => self::$appid,
            "nonceStr" => $nonceStr,
            "timestamp" => $timestamp,
            "url" => $url,
            "signature" => $signature,
            "rawString" => $string,
            "ticket" => $ticket,
            "token" => $access_token
        );
        // 返回数据给前端
        return $signPackage;
    }

    public static  function createNoncestr($length = 16) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str = "";
        for($i = 0; $i < $length; $i ++) {
            $str .= substr ( $chars, mt_rand ( 0, strlen ( $chars ) - 1 ), 1 );
        }
        return $str;
    }

    public static function curlHttp($url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
        curl_setopt($curl, CURLOPT_TIMEOUT, 500 );
        curl_setopt($curl, CURLOPT_URL, $url );
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,false);
        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }
}